package example.util;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

public class Base128 {
    private static final char[] CHARACTER_SET = {
            '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
            '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
            // 这里是补充的字符，确保字符在字符集中是唯一且不重复的
            '我', '是', '一', '个', '示', '例', '字', '符', '集',
            '你', '好', '世', '界', '再', '见', '欢', '迎', '谢', '谢', '春', '夏', '秋', '冬',
            '山', '水', '花', '草', '鸟', '兽', '云', '月', '星', '阳', '雨', '风', '雪',
            '狗', '猫', '鱼', '鸡', '鸭', '牛', '羊', '马', '兔', '猪', '狮', '虎', '豹', '熊', '蛇', '龙',
            '苹', '果', '橘', '子', '葡', '萄', '柠', '檬', '草', '莓', '蓝', '莓', '西', '瓜', '蜜', '瓜',
            '日', '月', '星', '辰', '云', '雨', '雷', '电', '山', '川', '河', '海', '瀑', '布', '森', '林'
    };

    @Test
    public void t2() throws Exception {
        System.out.println(CHARACTER_SET.length);
    }

    public static String encode(byte[] data) {
        StringBuilder encodedStr = new StringBuilder();
        for (byte b : data) {
            int value = b & 0xFF;  // 将字节转换为0-255的无符号整数
            encodedStr.append(CHARACTER_SET[value % CHARACTER_SET.length]);
        }
        return encodedStr.toString();
    }

    public static byte[] decode(String encodedStr) {
        byte[] decodedData = new byte[encodedStr.length()];
        for (int i = 0; i < encodedStr.length(); i++) {
            char c = encodedStr.charAt(i);
            int value = getIndex(c);
            decodedData[i] = (byte) value;
        }
        return decodedData;
    }

    private static int getIndex(char c) {
        for (int i = 0; i < CHARACTER_SET.length; i++) {
            if (CHARACTER_SET[i] == c) {
                return i;
            }
        }
        throw new IllegalArgumentException("Invalid character: " + c);
    }

    static String ORG_PATH = "D:/TEMP/20220618015052.png";
    private static final String FILE_ENCODING = "GBK";

    @Test
    public void t3() throws Exception {
        byte[] bytes = FileUtils.readFileToByteArray(FileUtils.getFile(ORG_PATH));
        String encode = encode(bytes);
        System.out.println(encode);
        FileUtils.writeStringToFile(FileUtils.getFile("C:/Users/yangyang/Desktop/view128.txt"), encode, FILE_ENCODING);
    }

    @Test
    public void b2() throws Exception {
        String fileToString = FileUtils.readFileToString(FileUtils.getFile("C:/Users/yangyang/Desktop/view128.txt"), FILE_ENCODING);

        byte[] decode = decode(fileToString);

        FileUtils.writeByteArrayToFile(FileUtils.getFile("C:/Users/yangyang/Desktop/view128.png"), decode);

    }

}
